home *** CD-ROM | disk | FTP | other *** search
- /********************************************************************
- * lindner
- * 3.3
- * 1993/04/09 16:50:24
- * /home/mudhoney/GopherSrc/CVS/gopher+/gopherd/command.c,v
- * Exp
- *
- * Paul Lindner, University of Minnesota CIS.
- *
- * Copyright 1991, 1992, 1993 by the Regents of the University of Minnesota
- * see the file "Copyright" in the distribution for conditions of use.
- *********************************************************************
- * MODULE: command.c
- * Routines to parse commands from the client.
- *********************************************************************
- * Revision History:
- * command.c,v
- * Revision 3.3 1993/04/09 16:50:24 lindner
- * nothing
- *
- * Revision 3.2 1993/03/24 20:19:40 lindner
- * Fixed memory leak
- *
- * Revision 3.1 1993/03/19 19:56:53 lindner
- * New CMD object
- *
- *
- *********************************************************************/
-
-
-
- #include "Malloc.h"
- #include "String.h"
- #include <stdio.h>
-
- #include "command.h"
- #include "openers.h"
-
- extern int DEBUG;
-
- CMDobj *
- CMDnew()
- {
- CMDobj *temp;
-
- temp = (CMDobj *) malloc(sizeof(CMDobj));
-
- temp->selstr = temp->command = temp->search = NULL;
-
- temp->datafromnet = STRnew();
- temp->askfile = STRnew();
-
- temp->secureuser = STRnew();
- temp->ticket = STRnew();
-
- CMDsetGplus(temp, FALSE);
-
- return(temp);
- }
-
-
- void
- CMDdestroy(cmd)
- CMDobj *cmd;
- {
- STRdestroy(cmd->datafromnet);
- STRdestroy(cmd->askfile);
- STRdestroy(cmd->ticket);
- STRdestroy(cmd->secureuser);
-
- free(cmd);
- }
-
-
- void
- CMDfromNet(cmd, sockfd)
- CMDobj *cmd;
- int sockfd;
- {
- char inputline[512];
- char *cp, *incoming;
- char *field1=NULL, *field2=NULL, *field3 = NULL;
- char *extradata = NULL;
- int length;
-
- length = readline(sockfd, inputline, sizeof(inputline));
-
- /** Set the alarm signal for about an hour, just in case.. **/
- (void) alarm(60 * 60);
-
-
- if (length <= 0) {
- close(sockfd);
- err_quit("getcommand: readline error");
- }
-
- ZapCRLF(inputline);
-
- if (DEBUG)
- printf("\nReceived: %s\n", inputline);
-
- CMDsetData(cmd, inputline);
-
- cp = CMDgetData(cmd);
-
-
- cp = CMDticketfromLine(cmd, cp);
-
- CMDsetSelstr(cmd, cp);
-
-
- /** Find the first field, if it exists... **/
- cp = strchr(cp, '\t');
- if (cp != NULL) {
- *cp = '\0';
- cp++;
-
- field1 = cp;
-
- /** find the second field, if it exists **/
- cp = strchr(cp, '\t');
- if (cp != NULL) {
- *cp = '\0';
- cp++;
- field2 = cp;
- } else {
- /** find the third field, if it exists **/
- if (cp != NULL)
- cp = strchr(cp, '\t');
- if (cp != NULL) {
- *cp = '\0';
- cp++;
- field3 = cp;
- }
- }
-
- }
- /** Okay, now decide which field is the search and
- which is the command */
-
- if (*inputline == '7' || strncmp(inputline, "waissrc:",8)==0 ||
- strncmp(inputline, "mindex:",7) ==0) {
-
- CMDsetSearch(cmd, field1);
- CMDsetCommand(cmd, field2);
- } else {
- CMDsetCommand(cmd, field1);
- CMDsetSearch(cmd, NULL);
- }
-
- /** Get the extra data if it exists... **/
- if (field3 != NULL)
- extradata = field3;
- else if (CMDgetSearch(cmd) == NULL && field2 != NULL)
- extradata = field2;
-
- if (extradata != NULL) {
- char *tmp = tempnam(NULL, "gdata");
- if (DEBUG)
- printf("Ask data is in %s\n", tmp);
- CMDsetAskfile(cmd, tmp);
- CMDgetXtra(cmd, sockfd, atoi(extradata));
- free(tmp);
- }
-
- if (CMDgetCommand(cmd) != NULL && *CMDgetCommand(cmd) != '\0')
- CMDsetGplus(cmd, TRUE);
-
- if (DEBUG)
- printf("Command:: selstr %s, command %s, search %s, extradata %s, user %s, ticket %s\n",
- CMDgetSelstr(cmd),
- CMDgetCommand(cmd),
- CMDgetSearch(cmd),
- CMDgetAskfile(cmd),
- CMDgetUser(cmd),
- CMDgetTicket(cmd));
- }
-
-
- /*
- * Retrieve extra data from the client request.. This stuff is optional
- *
- */
-
- void
- CMDgetXtra(cmd, fd, extradata)
- CMDobj *cmd;
- int fd;
- int extradata;
- {
- FILE *retrfile;
- char inputline[512];
-
- /** Siphon off data if it's there.. **/
-
- /** A ticket? **/
- if ((extradata & 0x2) == 0x2) {
- ;
- }
-
- /** An ask block **/
- if ((extradata & 0x1) == 0x1) {
- retrfile = ufopen(CMDgetAskfile(cmd), "w",0777);
-
- /** Okay, the next line is either +-1, or +bytes .. **/
- readline(fd, inputline, sizeof(inputline));
- ZapCRLF(inputline);
- if (strcmp(inputline, "+-1")==0) {
- while (readline(fd, inputline, sizeof(inputline))>0) {
- ZapCRLF(inputline);
- ZapCRLF(inputline);
- if (*inputline == '.' && *(inputline+1) == '\0')
- break;
- fputs(inputline, retrfile);
- putc('\n', retrfile);
- }
- }
- }
- fclose(retrfile);
- }
-
-
-
- char *
- CMDticketfromLine(cmd, input)
- CMDobj *cmd;
- char *input;
- {
- char *cp;
- char *originput = input;
-
- if (strncmp(input, "*UMNDES ",8)!=0)
- return(originput);
-
- input+=8;
-
- cp = strchr(input, ' ');
- if (cp == NULL)
- return(originput);
-
- *cp = '\0';
-
- CMDsetUser(cmd,input);
-
- if (strlen(CMDgetUser(cmd)) == 0)
- return(originput);
-
- /** Special cases for icky turbogopher **/
-
- if (strlen(cp+1) < 16) {
- input = strchr(cp+1, '\t');
- *(input-1) = '\0';
- CMDsetTicket(cmd, cp+1);
- return(input);
- } else if (*(cp+17) == ' ') {
- *(cp+17) = '\0';
- CMDsetTicket(cmd, cp+1);
- return(cp+18);
- } else {
- CMDsetTicket(cmd, cp+1);
- *(CMDgetTicket(cmd)+17) = '\0';
- return(cp+17);
- }
-
- }
-
-